﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Domain.Infrastructure;
using VOL.Domain.IServices.ProductionManagement;
using VOL.Entity.DomainModels;

namespace VOL.Domain.Services.ProductionManagement
{
    /// <summary>
    /// 生产工单
    /// </summary>
    public class WorkOrderService : IWorkOrderService
    {
        private readonly IBaseRepository<WorkOrder> _workOrderRepository;
        public WorkOrderService(IBaseRepository<WorkOrder> workOrderRepository)
        {
            _workOrderRepository = workOrderRepository;
        }
        /// <summary>
        /// 生产工单新增
        /// </summary>
        /// <param name="workOrder"></param>
        /// <returns></returns>
        public async Task<int> AddWorkOrder(WorkOrder workOrder)
        {
            return await _workOrderRepository.Add(workOrder);
        }
        /// <summary>
        /// 生产工单删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteWorkOrder(int id)
        {
            var workOrder = (await _workOrderRepository.FindAsync(m => m.Id == id)).FirstOrDefault();
            if (workOrder == null) return 0;
            return await _workOrderRepository.Delete(workOrder);
        }
        /// <summary>
        /// 生产工单显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<WorkOrder>> GetAllWorkOrder(int pageIndex = 1, int pageSize = 5)
        {
            var workOrderList = _workOrderRepository.GetAll();
            var total = workOrderList.Count();
            var items = workOrderList.OrderBy(m => m.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return workOrderList.ToList();
        }
        /// <summary>
        /// 生产工单修改
        /// </summary>
        /// <param name="workOrder"></param>
        /// <returns></returns>
        public async Task<int> UpdateWorkOrder(WorkOrder workOrder)
        {
            return await _workOrderRepository.Update(workOrder);
        }
    }
}
